﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RenameEpisodes
{
    class RenameEpisodesJob
    {
        public bool TestMode = true;
        private string path;
        private string pattern;
        private string fileName;
        private Mode mode;
        private EpisodeNameList episodeNameList;
        public RenameEpisodesJob(Mode mode, string path, string pattern, string fileName)
        {
            this.mode = mode;
            this.path = path;
            this.pattern = pattern;
            this.fileName = fileName;
        }
        public void Start()
        {
            if (mode == Mode.File)
            {
                episodeNameList = new EpisodeNameList(fileName);
            }
            ProcessFolder(path);
        }
        private void ProcessFolder(string folder)
        {
            List<string> ficheros = new List<string>();
            FillList(ficheros, folder, "*.avi");
            FillList(ficheros, folder, "*.mkv");
            FillList(ficheros, folder, "*.srt");
            foreach (string file in ficheros)
            {
                Renombra(file);
            }
            

        }

        private void Renombra(string file)
        {
            EpisodeId chapter = new EpisodeId();
            if (chapter.ParseString(file))
            {
                FileInfo fi = new FileInfo(file);
                string chapterID= string.Format("{0}x{1}", chapter.Season, chapter.Chapter.ToString("D2"));
                string newName = null;
                switch (mode)
                {
                    case Mode.Pattern:
                        newName = string.Format("{0}\\{1}{2}", path, string.Format(pattern, chapterID), fi.Extension);
                        break;
                    case Mode.File:
                        if (episodeNameList.List.Count() < chapter.Chapter)
                        {
                            newName = string.Format("{0}\\{1}{2}", path, chapterID + " Sin nombre", fi.Extension);
                        }
                        else
                        {
                            newName = string.Format("{0}\\{1}{2}", path, chapterID + " " +episodeNameList.List[chapter.Chapter-1] , fi.Extension);
                        }
                        break;
                }
                
                try
                {
                    FileInfo f1, f2;
                    f1 = new FileInfo(file);
                    f2 = new FileInfo(newName);
                    Console.WriteLine("{0} \t=> {1}", f1.Name, f2.Name);
                    if (!TestMode)
                        File.Move(file, newName);
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("Error in renaming operation: {0}", e);
                }
            }
        }
        private void FillList(List<string> fileList, string path, string pattern)
        {
            fileList.AddRange(Directory.GetFiles(path, pattern, SearchOption.TopDirectoryOnly));
        }
    }
}
